package com.woniuxy.school.dietary.store.controller;


import com.woniuxy.commons.core.model.Result;
import com.woniuxy.commons.core.util.JwtUtil;
import com.woniuxy.school.dietary.store.entity.StoreInfo;
import com.woniuxy.school.dietary.store.model.dto.FindStoreByIdDto;
import com.woniuxy.school.dietary.store.model.dto.FindStoreDto;
import com.woniuxy.school.dietary.store.model.from.AddStoreInfoForm;
import com.woniuxy.school.dietary.store.model.from.FindStoreFrom;
import com.woniuxy.school.dietary.store.model.from.StoreRegisterForm;
import com.woniuxy.school.dietary.store.model.from.StoreTimeForm;
import com.woniuxy.school.dietary.store.service.StoreInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.cloud.openfeign.SpringQueryMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import javax.validation.Valid;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 吴彦祖
 * @since 2025年05月14日
 */
@RestController
@RequestMapping("/storeInfo")
@Api(tags = "商家信息模块")
public class StoreInfoController {
    @Resource
    private StoreInfoService storeInfoService;
    /**
     * 查询商家信息
     */
    @GetMapping("/findStore")
    @ApiOperation(value = "查询商家信息")
    public Result<FindStoreDto> findStore(@PathVariable  Long storeId){
        FindStoreDto dto=storeInfoService.findStore(storeId);
        return Result.getResult(FindStoreDto.class).setCode(200).setMessage("查询商家信息成功").setData(dto);
    }
    @GetMapping("/findStoreById/{id}")
    @ApiOperation(value = "根据id查询商家姓名和手机号")
    public Result<FindStoreByIdDto> findStoreById(@PathVariable Long id){
        FindStoreByIdDto dto=storeInfoService.findStoreById(id);
        return Result.getResult(FindStoreByIdDto.class).setCode(200).setMessage("查询商家信息成功").setData(dto);
    }
    @GetMapping("/findAllStore")
    @ApiOperation(value = "查询所有商家信息")
    public Result<List<StoreInfo>> findAllStore(){
        List<StoreInfo> list=storeInfoService.list();
        return Result.getListResult(StoreInfo.class).setCode(200).setMessage("查询所有商家信息成功").setData(list);
    }
    @GetMapping("/findMyStoreInfo")
    @ApiOperation(value = "查询当前登录商家信息")
    public Result<StoreInfo> findMyStoreInfo(@RequestHeader String token) throws Exception{
        String userId = JwtUtil.parseUserToken(token).get("userId").toString();
        StoreInfo storeInfo=storeInfoService.getById(userId);
        return Result.getResult(StoreInfo.class).setCode(200).setMessage("查询当前登录商家信息成功").setData(storeInfo);
    }
    @PostMapping("/addStoreTime")
    @ApiOperation(value = "添加商家营业时间")
    public Result addStoreTime(@RequestBody StoreTimeForm storeTimeForm,@RequestHeader String token) throws Exception {
        String userId = JwtUtil.parseUserToken(token).get("userId").toString();
        storeInfoService.addStoreTime(storeTimeForm,userId);
        return Result.getResult().setCode(200).setMessage("添加成功");
    }
    @PostMapping("/updateStoreTime")
    @ApiOperation(value = "修改商家营业时间")
    public Result updateStoreTime(@RequestBody StoreTimeForm storeTimeForm,@RequestHeader String token) throws Exception {
        String userId = JwtUtil.parseUserToken(token).get("userId").toString();
        storeInfoService.updateStoreTime(storeTimeForm,userId);
        return Result.getResult().setCode(200).setMessage("修改成功");
    }
    @ApiOperation(value = "根据商家id查询商家信息")
    @PostMapping("/findStoreById")
    public Result<StoreInfo> findStoreByIds(@RequestBody Long storeId){
        StoreInfo storeInfo=storeInfoService.getById(storeId);
        return Result.getResult(StoreInfo.class).setCode(200).setMessage("查询成功").setData(storeInfo);
    }
    @ApiOperation(value = "根据类别id查询商家信息")
    @PostMapping("/findStoreByProductsId")
    public Result<List<StoreInfo>> findStoreByProductsId(@RequestBody Long productsId){
        List<StoreInfo> list=storeInfoService.findStoreByProductsId(productsId);
        return Result.getListResult(StoreInfo.class).setCode(200).setMessage("查询成功").setData(list);
    }
    @ApiOperation(value = "添加商家信息")
    @PostMapping("/addStoreInfo")
    public Result addStoreInfo(@RequestBody AddStoreInfoForm addStoreInfoForm, @RequestHeader String token) throws Exception {
        String userId = JwtUtil.parseUserToken(token).get("userId").toString();
        storeInfoService.addStoreInfo(addStoreInfoForm,userId);
        return Result.getResult().setCode(200).setMessage("添加成功");
    }

}

